// pages/shop/address/add/add.js
var app = getApp();
import Api from '../../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    valueArray:[0,0,0],
    cityName:'请选择所在的城市',
    cityArray:[],
    name:"",
    mobile:"",
    detailAddress:"",
    isCanDo:true,
    isMoRen:true,
    cityArray:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getAddressList(){
    var _this = this;
    var map = {};
    api.getRequestData(app.getCitysUrl, map, false).then(res => {
      var str = "'" + res.data.result+"'";
      var arr = JSON.parse(str);
      console.log("=====>>>>", arr);

      if (res.data.flag) {
      } else {
        app.alert(res.data.msg);
      }
    }).catch(err => {
        console.log(err);
    });
  },
  addAddress(){
    if (!this.data.isCanDo){
      return;
    }
    var _this = this;
    if (!this.data.name.length) {
      app.alert("请输入姓名");
      return;
    }
    if (!this.data.mobile.length) {
      app.alert("请输入手机号");
      return;
    }
    if (!(/^[1][3,6,4,5,7,8,9][0-9]{9}$/.test(this.data.mobile))) {
      app.alert("手机号码格式错误");
      return;
    }
    if (!this.data.cityName.length) {
      app.alert("请选择所在的城市");
      return;
    }
    if (!this.data.detailAddress.length) {
      app.alert("请输入详细地址");
      return;
    }
    var map = {};
    map.mobile = this.data.mobile;
    map.name = this.data.name;
    map.city = this.data.cityArray.join(" ");
    map.appId = app.APP_ID;
    map.userId = app.USER_ID();
    map.openId = app.OPEN_ID();
    if (this.data.isMoRen){
      map.isDefault = "1";
    } else {
      map.isDefault = "0";
    }
    map.address = this.data.detailAddress;
    console.log(JSON.stringify(map));
    this.data.isCanDo = false;
    api.getRequestData(app.addressSaveUrl, map, false).then(res => {
      if (res.data.errorCode == 0) {
        app.showToasts("添加成功");
        app.settlementPageNeedFresh = true;
        var page = getCurrentPages();
        var upPage = page[page.length - 2];
        upPage.setData({
          isNeedFresh: true
        });
        setTimeout(function(){
          _this.data.isCanDo = true;
          wx.navigateBack();
        },1500);
      } else {
        _this.data.isCanDo = true;
        app.alert(res.data.errorMsg);
      }
    }).catch(err => {
      _this.data.isCanDo = true;
    });
  },
  switchChange(e){
    console.log(e.detail.value);
    this.data.isMoRen = e.detail.value;
  },
  nameInput(e){
    this.data.name = e.detail.value;
  },
  mobileInput(e) {
    this.data.mobile = e.detail.value;
  },
  detailInput(e) {
    this.data.detailAddress = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  selectCityClick(e){
    var value = e.detail.value;
    this.data.cityArray = value;
    this.setData({
      cityName: value.join(" "),
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})